package top.tangyh.lamp.care.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.aliyun.oss.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.tangyh.basic.base.R;
import top.tangyh.lamp.care.mapper.BPipeObsvRecordMapper;
import top.tangyh.lamp.care.service.BPipeObsvRecordService;
import top.tangyh.basic.base.service.impl.SuperServiceImpl;
import top.tangyh.lamp.care.manager.BPipeObsvRecordManager;
import top.tangyh.lamp.care.entity.BPipeObsvRecord;
import top.tangyh.lamp.care.vo.query.PipeObsvRecorddLatestParam;
import top.tangyh.lamp.care.vo.query.QueryPipeObsvRecorddParam;
import top.tangyh.lamp.care.vo.result.BPipeObsvRecordResultVO;
import top.tangyh.lamp.care.vo.result.PageOutputDTO;
import top.tangyh.lamp.care.vo.result.PipeLatestObsvRecordDTO;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 业务实现类
 * 导管观察记录表
 * </p>
 *
 * @author zqb
 * @date 2024-07-19 15:02:24
 * @create [2024-07-19 15:02:24] [zqb] [代码生成器生成]
 */
@Slf4j
@RequiredArgsConstructor
@Service
@Transactional(readOnly = true)
public class BPipeObsvRecordServiceImpl extends SuperServiceImpl<BPipeObsvRecordManager, Long, BPipeObsvRecord> implements BPipeObsvRecordService {

    private final BPipeObsvRecordMapper bPipeObsvRecordMapper;

    @Override
    public PageOutputDTO<BPipeObsvRecordResultVO> queryPageOfPipeObsvRecord(QueryPipeObsvRecorddParam query) {

        PageHelper.startPage(query.getPage(), query.getSize());

        if (null != query.getRecordId()) {
            QueryWrapper<BPipeObsvRecord> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq(null != query.getRecordId(), "record_id", query.getRecordId());
            queryWrapper.eq(!StringUtils.isNullOrEmpty(query.getPatientIdent()), "patient_ident", query.getPatientIdent());
            if (query.getType() != null && query.getType() == 1) {
                DateTime dateTime = DateUtil.beginOfDay(new Date());
                Date start = dateTime.toJdkDate();
                Date end = new Date(dateTime.getTime() + 24 * 60 * 60 * 1000);
                queryWrapper.lambda().between(BPipeObsvRecord::getObsvTime, start, end);
            }
            queryWrapper.orderByDesc("obsv_time");
            List<BPipeObsvRecord> list = bPipeObsvRecordMapper.selectList(queryWrapper);
            PageInfo pageInfo = new PageInfo<>(list);
            return new PageOutputDTO<>(pageInfo.getTotal(), (long) pageInfo.getPages(), (long) pageInfo.getPageNum(),

                    BeanUtil.copyToList(pageInfo.getList(), BPipeObsvRecordResultVO.class));
        } else {
            List<BPipeObsvRecord> list = new ArrayList<>();
            PageInfo pageInfo = new PageInfo<>(list);
            return new PageOutputDTO<>(pageInfo.getTotal(), (long) pageInfo.getPages(), (long) pageInfo.getPageNum(),
                    BeanUtil.copyToList(pageInfo.getList(), BPipeObsvRecordResultVO.class));
        }


    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addOrUpdateYxPipeObsvRecord(BPipeObsvRecord yxPipeObsvRecordPO) {
        if (null != yxPipeObsvRecordPO.getId()) {
            BPipeObsvRecord old = this.getById(yxPipeObsvRecordPO.getId());
            //判断本人是否可以修改
//            if(null != SecurityUtil.checkRecordAuth(old.getCreateBy() ,old.getUpdateBy())){
//                throw new BadRequestException(SecurityUtil.checkRecordAuth(old.getCreateBy() ,old.getUpdateBy()));
//            }
        }
        return bPipeObsvRecordMapper.insertOrUpdate(yxPipeObsvRecordPO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteYxPipeObsvRecord(Long id) {
        if (null != id) {
            BPipeObsvRecord old = this.getById(id);
//            if(null != SecurityUtil.checkRecordAuth(old.getCreateBy() ,old.getUpdateBy())){
//                throw new BadRequestException(SecurityUtil.checkRecordAuth(old.getCreateBy() ,old.getUpdateBy()));
//            }
        }
        return bPipeObsvRecordMapper.deleteById(id);
    }

    @Override
    public PipeLatestObsvRecordDTO getPipeObsvLatestRecord(PipeObsvRecorddLatestParam yxPipeObsvRecorddLatestParam) {
        List<PipeLatestObsvRecordDTO> list;
        if (null != yxPipeObsvRecorddLatestParam) {
            list = bPipeObsvRecordMapper.selectLatestRecord(yxPipeObsvRecorddLatestParam.getPatientIdent(),
                    yxPipeObsvRecorddLatestParam.getSearchPpName(), yxPipeObsvRecorddLatestParam.getSearchPpTypeName());
        } else {
            list = bPipeObsvRecordMapper.selectLatestRecord(null, null, null);
        }
        if (CollectionUtil.isNotEmpty(list)) {
            return list.get(0);
        }
        return null;
    }
}


